package lt.ntec.sudokusolver.engine;

public class Board {
	private Group[][] _groups = new Group[3][3];
	private Column[] _columns = new Column[9];
	private Row[] _rows = new Row[9];
	private Cell[][] _cellsB = new Cell[9][9];
	private Cell[] _cells = new Cell[81];

	public Board(int[][] nums) {
		int i = 0;
		for (int xx = 0; xx < 3; xx++) {
			for (int yy = 0; yy < 3; yy++) {
				_groups[xx][yy] = new Group();

				for (int x = 0; x < 3; x++) {
					for (int y = 0; y < 3; y++) {
						if (_rows[yy * 3 + y] == null) {
							_rows[yy * 3 + y] = new Row();
						}

						if (_columns[xx * 3 + x] == null) {
							_columns[xx * 3 + x] = new Column();
						}

						Cell cell = new Cell(nums[xx * 3 + x][yy * 3 + y],
								_groups[xx][yy], _rows[yy * 3 + y], _columns[xx
										* 3 + x]);

						_cellsB[xx * 3 + x][yy * 3 + y] = cell;
						_cells[i++] = cell;
						_groups[xx][yy].addCell(cell);
						_rows[yy * 3 + y].addCell(cell);
						_columns[xx * 3 + x].addCell(cell);
					}
				}
			}
		}
	}

	public void setNums(int[][] nums) {
		for (int x = 0; x < 9; x++) {
			for (int y = 0; y < 9; y++) {
				_cellsB[x][y].setNumber(nums[x][y]);
			}
		}
	}

	public Cell[] getCells() {
		return _cells;
	}

	public boolean isEmptyCells() {
		for (Cell cell : getCells()) {
			if (cell.getNumber() == 0) {
				return true;
			}
		}
		return false;
	}

	public int[][] getNums() {
		int [][] nums = new int[9][9];
		for (int x = 0; x < 9; x++) {
			for (int y = 0; y < 9; y++) {
				nums[x][y] = _cellsB[x][y].getNumber();
			}
		}
		return nums;
	}
}
